/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.IsfdServerDataQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdServerManagementQueryBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.service.IsfdServerManagementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务管理
 *
 * @date: 2025/2/21 16:00
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/server")
public class IsfdServerManagementController extends BaseController {

    private final IsfdServerManagementService isfdServerManagementService;

    /**
     * 新增服务
     */
    @PostMapping("/add")
    @Operation(summary = "服务添加", security = @SecurityRequirement(name = "isfd:server:add"))
    public R<Void> addServer(HttpServletRequest request, @RequestBody IsfdServerManagementAddBo isfdServerManagementAddBo) {
        return toAjax(isfdServerManagementService.add(request,isfdServerManagementAddBo));
    }
    /**
     * 删除服务
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除服务", security = @SecurityRequirement(name = "isfd:server:delete"))
    public R<Void> deleteServer(@PathVariable("id") Long id) {
        return toAjax(isfdServerManagementService.delete(id));
    }
    /**
     * 修改服务
     */
    @PostMapping("/update")
    @Operation(summary = "服务修改", security = @SecurityRequirement(name = "isfd:server:update"))
    public R<Void> updateServer(@RequestBody IsfdServerManagementAddBo isfdServerManagementAddBo) {
        return toAjax(isfdServerManagementService.update(isfdServerManagementAddBo));
    }
    /**
     * 查询服务
     */
    @PostMapping("/query")
    @Operation(summary = "服务查询", security = @SecurityRequirement(name = "isfd:server:query"))
    public R<IsfdServerManagementQueryVo> queryServer(@RequestBody IsfdServerManagementQueryBo isfdServerManagementQueryBo) {
        List<IsfdServerManagementVo> isfdServerManagementVos = isfdServerManagementService.query(isfdServerManagementQueryBo);
        Long total = isfdServerManagementService.getTotal(isfdServerManagementQueryBo);
        IsfdServerManagementQueryVo isfdServerManagementQueryVo = new IsfdServerManagementQueryVo();
        isfdServerManagementQueryVo.setTotal(total);
        isfdServerManagementQueryVo.setRecords(isfdServerManagementVos);
        return R.ok(isfdServerManagementQueryVo);
    }
    /**
     * 数据同步接口(导入)
     */
    @PostMapping("/import/{timeMills}/{modelId}")
    @Operation(summary = "数据同步(导入)", security = @SecurityRequirement(name = "isfd:server:sync"))
    public R<IsfdServerDataImportVo> syncServer(HttpServletRequest request,@PathVariable("modelId") Long  modelId,@RequestBody IsfdServerDataImportBo isfdServerDataImportBo) {
        isfdServerDataImportBo.setModelId(modelId);
        // 获取请求URL
        String url = request.getRequestURL().toString();
        isfdServerDataImportBo.setRequestUrl(url);
        return R.ok(isfdServerManagementService.serverDataImport(isfdServerDataImportBo));
    }
    /**
     * 数据同步接口(导出)
     */
    @GetMapping("/export/{timeMills}/{modelId}")
    @Operation(summary = "数据同步(导出)", security = @SecurityRequirement(name = "isfd:server:sync"))
    public R<IsfdServerDataExportVo> syncServer(HttpServletRequest request,@PathVariable("modelId") Long modelId,String createStartTime,
                                                String createEndTime,String updateStartTime, String updateEndTime,Integer pageSize, Integer pageNum) {
        IsfdServerDataExportBo isfdServerDataExportBo = new IsfdServerDataExportBo();
        isfdServerDataExportBo.setModelId(modelId);
        isfdServerDataExportBo.setCreateTimeStart(createStartTime);
        isfdServerDataExportBo.setCreateTimeEnd(createEndTime);
        isfdServerDataExportBo.setUpdateStartTime(updateStartTime);
        isfdServerDataExportBo.setUpdateEndTime(updateEndTime);
        isfdServerDataExportBo.setPageSize(pageSize);
        isfdServerDataExportBo.setPageNum(pageNum);
        // 获取请求URL
        String url = request.getRequestURL().toString();
        isfdServerDataExportBo.setRequestUrl(url);
        return R.ok(isfdServerManagementService.serverDataExport(isfdServerDataExportBo));
    }
    /**
     * 查询服务历史记录
     */
    @PostMapping("/history/list")
    @Operation(summary = "服务历史记录查询", security = @SecurityRequirement(name = "isfd:server:history"))
    public R<IsfdServerServiceHistroryListVo> queryServerHistory(@RequestBody IsfdServerServiceHistroryBo isfdServerServiceHistroryBo) {
        return R.ok(isfdServerManagementService.queryServerHistory(isfdServerServiceHistroryBo));
    }
    /**
     * 查询服务历史具体数据列表
     */
    @PostMapping("/history/data/list")
    @Operation(summary = "服务历史数据查询", security = @SecurityRequirement(name = "isfd:server:history"))
    public R<IsfdServerServiceHistroryDataVo> queryServerHistoryData(@RequestBody IsfdServerServiceHistroryBo isfdServerServiceHistroryBo) {
        return R.ok(isfdServerManagementService.queryServerHistoryData(isfdServerServiceHistroryBo));
    }
    /**
     * 查询服务所有历史记录数据详情
     */
    @PostMapping("/history/data/all")
    @Operation(summary = "查询服务所有历史记录数据详情", security = @SecurityRequirement(name = "isfd:server:history"))
    public R<List> queryServerHistoryDataAll(@RequestBody IsfdServerDataQueryBo isfdServerDataQueryBo) {
        return R.ok(isfdServerManagementService.queryServerHistoryDataAll(isfdServerDataQueryBo));
    }
    /**
     * 发布服务
     */
    @PostMapping("/publish/{id}")
    @Operation(summary = "服务发布", security = @SecurityRequirement(name = "isfd:server:publish"))
    public R<Void> publishServer(@PathVariable("id") Long id) {
        return toAjax(isfdServerManagementService.publish(id));
    }
    /**
     * 撤销服务
     */
    @PostMapping("/revoke/{id}")
    @Operation(summary = "服务撤销", security = @SecurityRequirement(name = "isfd:server:revoke"))
    public R<Void> revokeServer(@PathVariable("id") Long id) {
        return toAjax(isfdServerManagementService.revoke(id));
    }
}
